package com.weight.system.takecare.web;

import com.weight.system.takecare.PO.TakecareInfo;
import com.weight.system.takecare.VO.TakecareInfoVO;
import com.weight.system.takecare.service.TakecareInfoService;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import org.springframework.beans.BeanUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.servlet.ModelAndView;
import com.alibaba.fastjson.JSON;
import com.weight.common.util.JsonResultUtil;
import com.weight.common.util.CommonUtils;
import com.weight.common.util.DateFormUtils;
import com.weight.common.model.PageResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.BindingResult;
import com.weight.common.enums.ResultEnum;

/**
 * 设备保养记录表
 */
@RestController
@Slf4j
@RequestMapping("/takecare/takecareInfo")
public class TakecareInfoController {

	@Autowired
	private TakecareInfoService takecareInfoService;

	private String pattern = "yyyy-MM-dd";


	/**
	 * 1.查询设备保养记录表--列表页 
	 * @return
	 */
	@RequestMapping(value="/queryForTakecareInfoList",method=RequestMethod.GET)
	public ModelAndView queryForTakecareInfoList(){
			ModelAndView modelAndView = new ModelAndView();
			modelAndView.setViewName("/system/takecare/queryForTakecareInfoList");
			modelAndView.addObject("formID",CommonUtils.getUUId());
			return modelAndView;
	}

	/**
	 * 2.查询设备保养记录表--数据刷新  
	 * @param request
	 * @return
	 */
	@RequestMapping(value="/queryForTakecareInfoListDG",method=RequestMethod.GET)
	public String queryForTakecareInfoListDG(HttpServletRequest request){
			//1、获取查询条件
			Integer page = Integer.parseInt(request.getParameter("page"));
			Integer rows = Integer.parseInt(request.getParameter("rows"));
			
			String equmtId = request.getParameter("equmtId");
			String takecareDate = request.getParameter("takecareDate");
			String takecarePern = request.getParameter("takecarePern");
			String takecareDesc = request.getParameter("takecareDesc");

			Map<Object, Object> map =new HashMap<>();
			map.put("equmtId", equmtId);
			map.put("takecareDate", DateFormUtils.getStringToDate(takecareDate, pattern));
			map.put("takecarePern", takecarePern);
			map.put("takecareDesc", takecareDesc);

			
			//2、查询设备保养记录表列表
			List<TakecareInfo> takecareInfoList = takecareInfoService.findTakecareInfoByPage(page,rows,map);
			log.info("takecareInfoList={}",takecareInfoList);
			List<TakecareInfoVO> takecareInfoVOList = takecareInfoList.stream().map(e -> changePoToVO(e)).collect(Collectors.toList());
			//3、返回前台
			PageResult<TakecareInfoVO> result = new PageResult<TakecareInfoVO>(takecareInfoVOList);
			return  JSON.toJSONString(result);
	}

	/**
	 * 展示模板--转--保存模板  
	 * @param takecareInfoVO
	 * @param takecareInfo
	 * @return
	 */
	public void changeVOToPO(TakecareInfoVO takecareInfoVO,TakecareInfo takecareInfo){
			BeanUtils.copyProperties(takecareInfoVO,takecareInfo);
			takecareInfo.setTakecareDate(DateFormUtils.getStringToDate(takecareInfoVO.getTakecareDate(), pattern));
			

	}

	/**
	 * 保存模板--转--展示模板  
	 * @param takecareInfoVO
	 * @param takecareInfo
	 * @return
	 */
	public void changePoToVO(TakecareInfoVO takecareInfoVO,TakecareInfo takecareInfo){
			BeanUtils.copyProperties(takecareInfo,takecareInfoVO);
			takecareInfoVO.setTakecareDate(DateFormUtils.getDateToString(takecareInfo.getTakecareDate(), pattern));
			

	}

	/**
	 * 保存模板--转--展示模板  
	 * @param takecareInfo
	 * @return
	 */
	public TakecareInfoVO changePoToVO(TakecareInfo takecareInfo){
			TakecareInfoVO takecareInfoVO=new TakecareInfoVO();
			BeanUtils.copyProperties(takecareInfo,takecareInfoVO);
			takecareInfoVO.setTakecareDate(DateFormUtils.getDateToString(takecareInfo.getTakecareDate(), pattern));
			

			return takecareInfoVO;
	}

	/**
	 * 3.查询设备保养记录表--详情页
	 * @param takecareInfoId
	 * @return
	 */
	@RequestMapping("/queryForTakecareInfoDetail")
	public ModelAndView queryForTakecareInfoDetail(@RequestParam(value="takecareInfoId",required=false) String takecareInfoId){
			//1.校验id
			CommonUtils.validateExistID(takecareInfoId);
			
			//2.传参，查询
			Map<String, String> map=new HashMap<>();
			map.put("takecareInfoId", takecareInfoId);
			log.info("查询设备保养记录表入参，map={}",map);
			ModelAndView modelAndView = new ModelAndView();
			TakecareInfo takecareInfo = takecareInfoService.findByTakecareInfoId(map);
			
			TakecareInfoVO takecareInfoVO = changePoToVO(takecareInfo);
			//3、返回前台
			log.info("查询设备保养记录表查询成功：takecareInfo={}",takecareInfo);
			modelAndView.setViewName("/system/takecare/queryForTakecareInfoDetail");
			modelAndView.addObject("takecareInfoVO",takecareInfoVO);
			modelAndView.addObject("formID",CommonUtils.getUUId());
			return modelAndView;
	}

	/**
	 * 3.添加、修改设备保养记录表操作
	 * @param takecareInfoId
	 * @return
	 */
	@RequestMapping(value="/addTakecareInfo")
	public ModelAndView addTakecareInfo(@RequestParam(value="takecareInfoId",required=false) String takecareInfoId){
			//1、创建返回对象
			TakecareInfoVO takecareInfoVO = new TakecareInfoVO();
			
			//2.判定id不为空，则查询数据库，为修改操作
			if(StringUtils.isNotBlank(takecareInfoId)) {
				Map<String, String> map=new HashMap<>();
				map.put("takecareInfoId", takecareInfoId);
				log.info("修改操作，查询ID：takecareInfoId={}",takecareInfoId);
				TakecareInfo takecareInfo = takecareInfoService.findByTakecareInfoId(map);
				this.changePoToVO(takecareInfoVO,takecareInfo);
			}
			
			//3.数据返回给前台
			ModelAndView modelAndView = new ModelAndView();
			modelAndView.setViewName("/system/takecare/addTakecareInfo");
			modelAndView.addObject("takecareInfoVO",takecareInfoVO);
			modelAndView.addObject("formID",CommonUtils.getUUId());
			return modelAndView;
	}

	/**
	 * 5.保存 添加、修改设备保养记录表操作
	 * @param takecareInfoVO
	 * @param bindingResult
	 * @return
	 */
	@Transactional
	@RequestMapping(value="/saveTakecareInfo")
	public JsonResultUtil saveTakecareInfo(@Valid TakecareInfoVO takecareInfoVO,BindingResult bindingResult){
			String operateStr="add";
			//1.校验入参规则
			CommonUtils.validateBindingResult(bindingResult,takecareInfoVO);
			
			//2.获取id
			String takecareInfoId=takecareInfoVO.getTakecareInfoId();
			TakecareInfo takecareInfo=new TakecareInfo();
			BeanUtils.copyProperties(takecareInfoVO,takecareInfo);
			this.changeVOToPO(takecareInfoVO,takecareInfo);
			
			//3.判定id不为空，则为修改操作
			if(StringUtils.isNotBlank(takecareInfoId)) {
				operateStr="edit";
				Map<String, String> map=new HashMap<>();
				map.put("takecareInfoId", takecareInfoId);
				log.info("修改操作，查询ID：takecareInfoId={}",takecareInfoId);
				//查询是否存在
				takecareInfoService.findByTakecareInfoId(map);
				try{
					takecareInfo.setUpdateBy("000");
					takecareInfoService.updateTakecareInfo(takecareInfo);
					log.info("更新操作成功：takecareInfo={}",takecareInfo);
				}catch(Exception e){
					e.printStackTrace();
					log.error(e.getMessage(), e);
					return new JsonResultUtil(ResultEnum.EXCEPTION, "失败！", e.getMessage());
				}
			}else {//判定id为空，则为添加操作
				try{
					takecareInfo.setTakecareInfoId(CommonUtils.getUUId());
					takecareInfo.setCreateBy("000");
					takecareInfo.setUpdateBy("000");
					takecareInfoService.insertTakecareInfo(takecareInfo);
					log.info("添加操作成功：takecareInfo={}",takecareInfo);
				}catch(Exception e){
					e.printStackTrace();
					log.error(e.getMessage(), e);
					return new JsonResultUtil(ResultEnum.EXCEPTION, "失败！", e.getMessage());
				}
			}
			return new JsonResultUtil(ResultEnum.SUCCESS, "操作成功！", operateStr);
	}

	/**
	 * 6.删除设备保养记录表操作
	 * @param takecareInfoId
	 * @return
	 */
	@Transactional
	@RequestMapping("/deleteByTakecareInfoId")
	public JsonResultUtil deleteByTakecareInfoId(@RequestParam(value="takecareInfoId",required=false) String takecareInfoId){
			//1.校验id
			CommonUtils.validateExistID(takecareInfoId);
			
			//2.校验数据库中是否存在!
			Map<String, String> map=new HashMap<>();
			map.put("takecareInfoId", takecareInfoId);
			takecareInfoService.findByTakecareInfoId(map);
			//3.传参,添加操作人 删除
			map.put("updateBy","000");
			log.info("删除设备保养记录表入参，map={}",map);
			
			try{
				takecareInfoService.upDelByTakecareInfoId(map);
				return new JsonResultUtil(ResultEnum.SUCCESS, "删除成功！", null);  
			}catch(Exception e){
				e.printStackTrace();
				log.error(e.getMessage(), e);
				return new JsonResultUtil(ResultEnum.EXCEPTION, "操作失败！", e.getMessage());
			}
	}

	/**
	 * 6.删除设备保养记录表操作---批量删除
	 * @param takecareInfoIdList
	 * @return
	 */
	@Transactional
	@RequestMapping("/deleteByTakecareInfoIdBatch")
	public JsonResultUtil deleteByTakecareInfoIdBatch(@RequestParam(value="takecareInfoIdList",required=false) List takecareInfoIdList){
			//1.校验id
			CommonUtils.validateExistIDList(takecareInfoIdList);
			
			//2.校验数据库
			takecareInfoService.findByTakecareInfoIdList(takecareInfoIdList);
			//3.传参 
			Map<String, Object> map=new HashMap<>();
			map.put("takecareInfoIdList", takecareInfoIdList);
			map.put("updateBy","000");
			log.info("批量删除设备保养记录表操作入参，map={}",map);
			
			try{
				takecareInfoService.upDelByTakecareInfoIdBatch(map);
				return new JsonResultUtil(ResultEnum.SUCCESS, "批量删除成功！", null);  
			}catch(Exception e){
				e.printStackTrace();
				log.error(e.getMessage(), e);
				return new JsonResultUtil(ResultEnum.EXCEPTION, "操作失败！", e.getMessage());
			}
	}

}